package com.example.rescuer.zyfypt_926wyr.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.rescuer.zyfypt_926wyr.R;
import com.example.rescuer.zyfypt_926wyr.model.RegisterModel;

/**
 * Created by RESCUER on 2018/5/27.
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText etuser,etrealname,etpass,etrepass,etemail,ettel;
    private RadioButton etstudent,etteacher;
    private Button etcancel,etok;

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        context=RegisterActivity.this;
    }

    void initView(){
        etpass=(EditText)findViewById(R.id.etpass);
        etuser=(EditText)findViewById(R.id. etuser);
        etrepass=(EditText)findViewById(R.id.etrepass);
        ettel=(EditText)findViewById(R.id.ettel);
        etok=(Button)findViewById(R.id.etok);
        etcancel=(Button)findViewById(R.id.etcancel);
        etok.setOnClickListener(this);
        etcancel.setOnClickListener(this);
    }



    public void onResponse(String msg) {//网络访问成功，进入执行
        if(msg.contains("1")){//说明注册成功
            Toast.makeText(context,
                    "用户注册成功，可以登录",
                    Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(context,
                    "用户名已经存在，请重新注册",
                    Toast.LENGTH_SHORT).show();
        }
    }


    public void onFail(String s) {
        Toast.makeText(context,
                s,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.etok:
                String username=etuser.getText().toString();
                String password=etpass.getText().toString();
                String rePassword=etrepass.getText().toString();
                String tel=ettel.getText().toString();

                if (!password.equals(rePassword)){
                    etrepass.setError("密码不一致，请重新输入");
                }

                RegisterModel model=new RegisterModel();
                model.register(username,password,tel, this);
                break;
            case R.id.etcancel:
                Intent intent=new Intent(context,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}

